package com.dalin.platform.loa.action;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;

import com.dalin.common.PropertyConst;
import com.dalin.common.utils.StringUtils;
import com.dalin.framework.action.StrutsTwoAction;
import com.dalin.platform.loa.LoaConst;
import com.dalin.platform.loa.service.WsSecurityService;
import com.dalin.platform.loa.vo.LoginVo;
import com.dalin.platform.loa.vo.WsCurrentUserInfo;
import com.dalin.platform.loa.vo.WsDicItemVo;
import com.dalin.platform.pub.PlatContext;

/*****************************************************************************
 * @content: 登录验证                                                                                                                   *
 * @author : 那建林                                                                                                                     *
 * @crtDate: 2010-05-06                                                     *
 ****************************************************************************/
public class LoginAction extends StrutsTwoAction {
	private WsSecurityService wsSecurityService;
	private LoginVo vo;

	public String execute() throws Exception {
		super.printInfo("", "===== this action begin");
		try {
			boolean isCookieLogin = true;
			if (vo == null) vo = new LoginVo(); 
			String loginName = getCookieValue("loginName");
			String loginPswd = getCookieValue("loginPswd");
			if (StringUtils.isBlank(loginName) || StringUtils.isBlank(loginPswd)) {
				isCookieLogin = false;
				loginName = vo.getLoginName();
				loginPswd = vo.getLoginPswd();
			} 
			if (StringUtils.isBlank(loginName) || StringUtils.isBlank(loginPswd)) {
				return LOGIN;
			} 
			if (!isCookieLogin) {
				Object randAuthCode = getReq().getSession()
						.getAttribute(LoaConst.RANDOM_AUTHENTICODE_SESSION_NAME);
				if (randAuthCode == null) {
					getReq().setAttribute("errorInfo", "验证码已过期, 有效期为"+LoaConst.RANDOM_AUTHENTICODE_EXPIRE+"秒");
					return LOGIN;
				} else {
					if (!randAuthCode.equals(vo.getValidaNum())) {
						getReq().setAttribute("errorInfo", "验证码错误");
						return LOGIN;
					}
				}
			}
			
			WsCurrentUserInfo cui = wsSecurityService.checkLogin(
					loginName, 
					loginPswd, getReq().getRemoteAddr());
			if (!StringUtils.isBlank(cui.getPrompt())) {
				getReq().setAttribute("errorInfo", cui.getPrompt());
				return LOGIN;
			} 
			getReq().getSession().setAttribute(PropertyConst.USER_SESSION_NAME,cui);
			getReq().getSession().setMaxInactiveInterval(1800); 

			getReq().setAttribute("realName", cui.getRealName());
			
			if (vo.isRememberMe()) {
				Cookie loginNameCookie = new Cookie("loginName", loginName);
				loginNameCookie.setMaxAge(LoaConst.COOKIE_EXPIRE);
				Cookie loginPswdCookie = new Cookie("loginPswd", loginPswd);
				loginPswdCookie.setMaxAge(LoaConst.COOKIE_EXPIRE);
				getRes().addCookie(loginNameCookie);
				getRes().addCookie(loginPswdCookie);
			}
			
			cacheAllDic(cui.getProjectId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		super.printInfo("", "===== this action end");
		return SUCCESS;
	}
	
	
	private void cacheAllDic(Long projectId) throws Exception {
		Class loaConstClass = LoaConst.class;
		Field dicListMapField = loaConstClass.getDeclaredField("dicListMap");
		dicListMapField.setAccessible(true);
		Map<String, List<WsDicItemVo>> dicListMap = (Map<String, List<WsDicItemVo>>) dicListMapField.get(loaConstClass);
		dicListMap.putAll(PlatContext.getDicService().queryAllProjectItems(projectId));
	}
	
	
	public LoginVo getVo() {
		return vo;
	}
	public void setVo(LoginVo vo) {
		this.vo = vo;
	}
	public WsSecurityService getWsSecurityService() {
		return wsSecurityService;
	}
	public void setWsSecurityService(WsSecurityService wsSecurityService) {
		this.wsSecurityService = wsSecurityService;
	}
}
